package code3;

/**
 * @author noob
 * @version 1.0
 * @date 2021/3/17 10:26
 *
 */
public class BinarySearchLower_lowerfloor {

    // < target,返回最大值索引
    // == target:返回最小索引
    public static <E extends Comparable<E>> int lowerfloor(E[] data,E target){

        int l =lower(data,target);
        if(l+1 < data.length && data[l+1].compareTo(target)==0){
            return l + 1;
        }
        return l;

    }



    public static <E extends Comparable<E>> int lower(E[] data,E target){

        int left = -1;
        int right = data.length-1;

        while (left < right){
            int mid = left + (right - left+1)/2;

            if (data[mid].compareTo(target) <0){
                //去右边找
                left = mid;
            }else{
                right = mid - 1;
            }
        }
        return left;


    }



    public static void main(String[] args) {
        Integer[] arr = {1,1,3,3,5,5};
        for (int i = 0; i <= 6 ; i++) {
            int search = BinarySearchLower_lowerfloor.lowerfloor(arr,i);
            System.out.println("查找元素:"+i+"对应的索引是"+search + " ");
        }
        System.out.println();
    }
}
